package com.infore.airsupervise.biz.impl;

import com.infore.airsupervise.app.Constants;
import com.infore.airsupervise.app.MsgConst;
import com.infore.airsupervise.bean.QuestionE;
import com.infore.airsupervise.biz.QuestionDealBiz;
import com.infore.airsupervise.http.BaseResponse;
import com.infore.airsupervise.http.HttpAPI;
import com.infore.airsupervise.http.HttpCallBack;
import com.infore.airsupervise.http.HttpUtil;
import com.infore.airsupervise.http.OnServerResponseListener;
import com.infore.airsupervise.utils.JSONUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MultipartBody;
import retrofit2.Call;

/**
 * @features: 问题报告 相关网络请求业务实现
 * @author: WuTao
 * @date: 2017/4/13 09:05
 */
public class QuestionDealBizImpl implements QuestionDealBiz {
    @Override
    public Call<BaseResponse> sendQuestionListRequest(final OnServerResponseListener<List<QuestionE>> listener) {
        Call<BaseResponse> call = HttpUtil.getHttpService().postParams(HttpAPI.GET_QUESTION_LIST, Collections.EMPTY_MAP);
        call.enqueue( new HttpCallBack(){
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);
                List<QuestionE> list = JSONUtil.parseList(response.getData(), Constants.RESPONSE_BEAN_LIST_KEY,QuestionE.class);
                listener.onSucceed(list, MsgConst.GET_DATA_SUCCESS);
            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode,MsgConst.GET_DATA_FAILED,desc);
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, desc);
                listener.onFailed(errorCode,title,desc);
            }
        });
        return call;
    }

    @Override
    public Call<BaseResponse> sendQuestionCommitRequest(String userId , QuestionE questionE, List<String> files,final OnServerResponseListener<BaseResponse> listener) {
        Map<String , Object> params = new HashMap<>();
        params.put("quertionTitle",questionE.getQuestionTitle());
        params.put("quertionNum",questionE.getQuestionNum());
        params.put("releaseTime",questionE.getReleaseTime());
        params.put("gridNum",questionE.getGridNum());
        params.put("questionAddress",questionE.getQuestionAddress());
        params.put("questionContent",questionE.getQuestionContent());
        params.put("userId",userId);
        List<MultipartBody.Part> param_part = HttpUtil.getInstance().paramsToBodyParts(params);
        List<MultipartBody.Part> files_part = HttpUtil.getInstance().filesToBodyParts("pic",files);
        Call<BaseResponse> call = HttpUtil.getHttpService().postFiles(HttpAPI.COMMIT_QUESTION,files_part,param_part);
        call.enqueue( new HttpCallBack() {
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);
                listener.onSucceed(response,MsgConst.OPERATION_SUCCESS);
            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode,MsgConst.OPERATION_FAILED,desc);
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, desc);
                listener.onFailed(errorCode,title,desc);
            }
        });
        return call;
    }
}
